<!--
 * Afrimesh: easy management for B.A.T.M.A.N. wireless mesh networks
 * Copyright (C) 2008-2009 Meraka Institute of the CSIR
 * All rights reserved.
 *  
 * This software is licensed as free software under the terms of the
 * New BSD License. See /LICENSE for more information.
-->

<h2>Devices</h2>

<!-- /* TODO - use the same caching mechanism as the map */
     /* TODO - write the caching mechanism for the map - at what level? villagebus? higher? */ -->
<div id="network-devices"></div>


<script type="text/javascript"> //<![CDATA[    
(function() {

  /** includes ------------------------------------------------------------ */
  /* load includes with: load("path/to/file.js");   */
  /* often a good idea:  load("modules/network.devices.js"); *;/

  /** construction -------------------------------------------------------- */
  ready = function() { // INJ 'ready' should be the only symbol we're exposing to the global namespace
    /* often a good place to do this: populate_control(); */
    console.debug("loaded network.devices.html");
    update();
    return unload;
  };

  function unload() {
    clearTimeout(timer);
    console.debug("unloaded network.devices.html");
  };


  /** populate control ---------------------------------------------------- */
  var timer = {};
  function update() {
    /* clearTimeout(timer); */

    /* TODO - afrimesh.network.devices.async(populate_devices); */
    var routers = afrimesh.network.routers();
    populate_devices(routers);

    /* timer = setTimeout(update, 5000); */
  };
  function populate_devices(routers) {
    /* get accounting info for routers */
    routers = afrimesh.network.accounting.routers(routers);
    var html = "<table class='network-devices' border=0>"
    html += "<thead><th>mac</th><th>address</th><th>packets</th><th>bytes</th><!-- th>version</th><th>uname</th --></thead>";
    html += "<tbody></tbody></table>";
    $("div#network-devices").html(html);
    var table = $("#network-devices table > tbody:last");
    var rows = "";
    routers.map(function(router) {
        var packets = (router.traffic.packets ? router.traffic.packets : "-");
        var bytes   = (router.traffic.bytes   ? pretty_bytes(router.traffic.bytes) : "-");
        rows += "<tr>";
        rows += "<td id='mac'>"     + (router.mac ? router.mac : "-")  + "</td>";
        rows += "<td id='address'>" + router.address + "</td>";
        rows += "<td id='traffic'>" + packets + "</td>";
        rows += "<td id='traffic'>" + bytes   + "</td>";
        //rows += "<td id='version'>" + "" /*afrimesh.villagebus.sys.version(router.address)*/ + "</td>";
        //rows += "<td id='uname'>uname</td>";
        // TODO - this should be cached, async & wrapped around the router like accounting above
        //rows += "<td id='uname'>"   + "" /*afrimesh.villagebus.sys.uname(router.address)*/ + "</td>";
        rows += "</tr>";
      });
    table.append(rows);
  };


  /** done ---------------------------------------------------------------- */
  console.debug("loaded network.devices.js");
 
})();
//]]></script>


